﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.Catalog 
{
    public partial class FrmGiaovien : PRE.Catalog.FrmBase
    {
        DAL.EntitesDAL.GiaovienDAL _dal = new DAL.EntitesDAL.GiaovienDAL();
        BLL.EntitiesBLL.GiaovienBLL _bll = new BLL.EntitiesBLL.GiaovienBLL();

        public FrmGiaovien()
        {
            InitializeComponent();
        }


        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            DataTable _ma = _dal.Select_Max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _bll.AddID(_ma.Rows[0]["Ma"] + "");
                    txtMaGV.Text = ma;

                    ReadOnlyControl(false);
                    ClearDataBindings();
                    ChangeStatus(false);

                    txtHoten.ResetText();
                    txtDiachi.ResetText();
                    txtDienthoai.ResetText();
                    txtTrinhdo.ResetText();
                    dteNamsinh.EditValue = DateTime.Now;

                    txtHoten.Focus();
                }
            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var id = grvMain.GetFocusedRowCellValue("MaGV") + "";

            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);

            txtHoten.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();

            // dxEp
            dxEP.SetError(txtHoten, "");
            dxEP.SetError(txtDienthoai, "");
            dxEP.SetError(txtDiachi, "");
            dxEP.SetError(txtTrinhdo, "");

            dteNamsinh.Enabled = true;
            radioGioitinh.Enabled = true;
            txtMaGV.Properties.ReadOnly = true;
         //   btnGoSearch.Visible = false;

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }



            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            var name = Proper(txtHoten.Text);
            var address = Proper(txtDiachi.Text);
            var trinhdo = Proper(txtTrinhdo.Text);
            if (IsAdd)
            {
                if (txtHoten.Text == "" || txtDienthoai.Text == "" || txtDiachi.Text == "" || txtTrinhdo.Text == "")
                {
                    MessageBox.Show("Bạn phải điền đầy đủ thông tin cho giáo viên !!!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    PerformAdd();
                }
                else
                {
                    var o = new DAL.Entities.Giaovien()
                    {
                        MaGV = txtMaGV.Text,
                        Hoten = name,
                        Namsinh = Convert.ToDateTime(dteNamsinh.EditValue),
                        Gioitinh = Convert.ToBoolean(radioGioitinh.EditValue),
                        Diachi = address,
                        Dienthoai = txtDienthoai.Text,
                        Trinhdo = trinhdo
                    };

                    var oki = _dal.Insert(o);
                    if (oki)
                    {
                        //MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        PerformRefresh();
                        ChangeStatus(false);
                        PerformAdd();
                    }
                    else
                    {
                        MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        ChangeStatus(false);
                        ReadOnlyControl(false);
                    }
                }
            }

            if (IsEdit)
            {
                var id = grvMain.GetFocusedRowCellValue("MaGV") + "";

                var o = new DAL.Entities.Giaovien()
                {
                    MaGV = id,
                    Hoten = name,
                    Namsinh = Convert.ToDateTime(dteNamsinh.EditValue),
                    Gioitinh = Convert.ToBoolean(radioGioitinh.EditValue),
                    Diachi = address,
                    Dienthoai = txtDienthoai.Text,
                    Trinhdo = trinhdo
                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    // MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {

            txtDiachi.Text = null;
            txtDienthoai.Text = null;
            txtTrinhdo.Text = null;
            txtHoten.Text = null;
            dteNamsinh.EditValue = DateTime.Now;
            radioGioitinh.EditValue = true;

            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtMaGV.DataBindings.Clear();
            txtHoten.DataBindings.Clear();
            dteNamsinh.DataBindings.Clear();
            radioGioitinh.DataBindings.Clear();
            txtDiachi.DataBindings.Clear();
            txtDienthoai.DataBindings.Clear();
            txtTrinhdo.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtMaGV.DataBindings.Add("EditValue", _dtb, ".MaGV");
            txtHoten.DataBindings.Add("EditValue", _dtb, ".HoTen");
            dteNamsinh.DataBindings.Add("EditValue", _dtb, ".Namsinh");
            radioGioitinh.DataBindings.Add("EditValue", _dtb, ".Gioitinh");
            txtDiachi.DataBindings.Add("EditValue", _dtb, ".Diachi");
            txtDienthoai.DataBindings.Add("EditValue", _dtb, ".Dienthoai");
            txtTrinhdo.DataBindings.Add("EditValue", _dtb, ".Trinhdo");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtMaHV.Properties.ReadOnly = isReadOnly;
            txtHoten.Properties.ReadOnly = isReadOnly;
            dteNamsinh.Properties.ReadOnly = isReadOnly;
            radioGioitinh.Properties.ReadOnly = isReadOnly;
            txtDiachi.Properties.ReadOnly = isReadOnly;
            txtDienthoai.Properties.ReadOnly = isReadOnly;
            txtTrinhdo.Properties.ReadOnly = isReadOnly;

            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            ResetText();
            ChangeStatus(false);
            ReadOnlyControl(false);
            ClearDataBindings();
            
            dxEP.SetError(txtHoten, "");
            dxEP.SetError(txtDienthoai, "");
            dxEP.SetError(txtDiachi, "");
            dxEP.SetError(txtTrinhdo, "");

          //  btnGoSearch.Visible = true;

            dteNamsinh.Enabled = false;
            radioGioitinh.Enabled = false;

            txtMaGV.Properties.ReadOnly = false;
            
            grcMain.Enabled = true;

            txtMaGV.Text = null;
            txtMaGV.Focus();
           // txtDienthoai.Properties.Mask.EditMask =;

            
            base.PerformSearch();
        }

        /// <summary>
        /// Execute Search data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void btnGoSearch_Click(object sender, EventArgs e)
        //{
        //    string name = Proper(txtHoten.Text);
        //    string address = Proper(txtDiachi.Text);
        //    string trinhdo = Proper(txtTrinhdo.Text);

        //        var o = new DAL.Entities.Giaovien()
        //        {
        //            MaGV = txtMaGV.Text,
        //            Hoten = name,
        //            Diachi = address,
        //            Dienthoai = txtDienthoai.Text,
        //            Trinhdo = txtTrinhdo.Text
        //        };
        //        grcMain.DataSource = _dal.Search(o);
            
        //}

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dtb = _dal.Select();

            //_dtb.WriteXml("C:\\a.xml");

            //_dtb.ReadXml("C:\\a.xml");

            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
                //gridColumn2.BestFit(); // fit column                
            }

            base.LoadData();
        }
        #endregion

        private void FrmGiaovien_Load(object sender, EventArgs e)
        {
            PerformRefresh();
        }


        #region TextChanged


        private void txtMaGV_TextChanged(object sender, EventArgs e)
        {

            // btnGoSearch_Click(sender, e);
        }

        private void txtHoten_TextChanged(object sender, EventArgs e)
        {
            //btnGoSearch_Click(sender, e);
        }

        private void txtDiachi_TextChanged(object sender, EventArgs e)
        {
            //  btnGoSearch_Click(sender, e);
        }

        private void txtDienthoai_TextChanged(object sender, EventArgs e)
        {
            // btnGoSearch_Click(sender, e);
        }

        private void txtTrinhdo_TextChanged(object sender, EventArgs e)
        {
            //   btnGoSearch_Click(sender, e);
        }

        #endregion


        #region Validating

        private void txtHoten_Validating(object sender, CancelEventArgs e)
        {
            if (txtHoten.Text + "" == "")
                dxEP.SetError(txtHoten, "Họ tên giáo viên không được trống !!!");
            else
                dxEP.SetError(txtHoten, "");
        }

        private void txtDienthoai_Validating(object sender, CancelEventArgs e)
        {
            if (txtDienthoai.Text + "" == "")
                dxEP.SetError(txtDienthoai, "Điện thoại giáo viên không được trống !!!");
            else
                dxEP.SetError(txtDienthoai, "");
        }

        private void txtDiachi_Validating(object sender, CancelEventArgs e)
        {
            if (txtDiachi.Text + "" == "")
                dxEP.SetError(txtDiachi, "Địa chỉ giáo viên không được trống !!!");
            else
                dxEP.SetError(txtDiachi, "");
        }

        private void txtTrinhdo_Validating(object sender, CancelEventArgs e)
        {
            if (txtTrinhdo.Text + "" == "")
                dxEP.SetError(txtTrinhdo, "Trình độ giáo viên không được trống !!!");
            else
                dxEP.SetError(txtTrinhdo, "");
        }

        #endregion

        



    }
}
